package com.ruoyi.bizsys.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.bizsys.vo.WalletFlowVo;
import com.ruoyi.common.utils.DateUtils;
            import org.springframework.stereotype.Service;
    import com.ruoyi.bizsys.mapper.WalletFlowMapper;
import com.ruoyi.bizsys.domain.WalletFlow;
import com.ruoyi.bizsys.service.IWalletFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 钱包流水Service业务层处理
 *
 * @author african-loan
 * @date 2022-06-17
 */
@Service
public class WalletFlowServiceImpl extends ServiceImpl<WalletFlowMapper,WalletFlow> implements IWalletFlowService {

    /**
     * 查询钱包流水
     *
     * @param id 钱包流水ID
     * @return 钱包流水
     */
    @Override
    public WalletFlow selectWalletFlowById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询钱包流水列表
     *
     * @param walletFlow 钱包流水
     * @return 钱包流水
     */
    @Override
    public List<WalletFlow> selectWalletFlowList(WalletFlow walletFlow) {
        if(walletFlow.getParams().entrySet().size() > 0){
            return this.list(new QueryWrapper<>(walletFlow).ge("create_time",walletFlow.getParams().get("beginTime")).lt("create_time",walletFlow.getParams().get("endTime")).last("order by id desc"));
        }
        return this.list(new QueryWrapper<>(walletFlow).last("order by id desc"));
    }

    /**
     * 新增钱包流水
     *
     * @param walletFlow 钱包流水
     * @return 结果
     */
            @Override
    public int insertWalletFlow(WalletFlow walletFlow) {
                                                                                                                                                                                                                                                                                                                                                walletFlow.setCreateTime(DateUtils.getNowDate());
                                                                    return baseMapper.insert(walletFlow);
    }

    /**
     * 修改钱包流水
     *
     * @param walletFlow 钱包流水
     * @return 结果
     */
            @Override
    public int updateWalletFlow(WalletFlow walletFlow) {
                                                                                                                                                                                                                                                                                                                                                                                        walletFlow.setUpdateTime(DateUtils.getNowDate());
                            return baseMapper.updateById(walletFlow);
    }

    /**
     * 批量删除钱包流水
     *
     * @param ids 需要删除的钱包流水ID
     * @return 结果
     */
            @Override
    public int deleteWalletFlowByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除钱包流水信息
     *
     * @param id 钱包流水ID
     * @return 结果
     */
    @Override
    public int deleteWalletFlowById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<WalletFlowVo> selectWalletInfoList(String custNo) {
        List<WalletFlowVo> walletFlowVos = baseMapper.selectWalletInfoList(custNo);
        ArrayList<WalletFlowVo> walletFlowList = new ArrayList<>();
        //DecimalFormat df2 = new DecimalFormat("#,###.00");//千分位格式化
        walletFlowVos.forEach(e ->{
            WalletFlowVo walletFlowVo = new WalletFlowVo();
            walletFlowVo.setChangeType(e.getChangeType());
            walletFlowVo.setFundsType(e.getFundsType());
            walletFlowVo.setWalletType(e.getWalletType());
            walletFlowVo.setChangeAmount("₦ "+ e.getAmount().setScale(2, BigDecimal.ROUND_UP));
            walletFlowVo.setCreateTime(e.getCreateTime());
            walletFlowList.add(walletFlowVo);
        });
        return walletFlowList;
    }
}
